package cn.tedu.learn.tea.adapter.controller;

import cn.tedu.learn.tea.adapter.assemble.ContentAssemble;
import cn.tedu.learn.tea.adapter.protocol.vo.ContentListVO;
import cn.tedu.learn.tea.adapter.protocol.vo.ContentVO;
import cn.tedu.learn.tea.domain.bo.ContentBO;
import cn.tedu.learn.tea.domain.bo.ContentListBO;
import cn.tedu.learn.tea.domain.common.constant.PageConstant;
import cn.tedu.learn.tea.domain.response.JsonPage;
import cn.tedu.learn.tea.domain.service.ContentService;
import cn.tedu.learn.tea.protocol.parm.ContentParam;
import cn.tedu.learn.tea.protocol.parm.ContentUpdateParam;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * 内容Controller
 */
@Api(value = "content", tags = "内容管理")
@Validated
@RestController
@RequestMapping("/content")
@Slf4j
public class ContentController {

    @Autowired
    private ContentService contentService;

    @Autowired
    private ContentAssemble contentAssemble;

    @ApiOperation("新增内容")
    @PostMapping("/save")
    public void addContent(@Valid ContentParam contentParam) {
        contentService.addContent(contentParam);
    }

    @ApiOperation("修改内容")
    @PostMapping("/update")
    public void updateContent(@Valid ContentUpdateParam contentUpdateParam) {
        contentService.updateContent(contentUpdateParam);
    }

    @ApiOperation("删除内容")
    @ApiImplicitParams(
            {@ApiImplicitParam(name = "contentId", value = "内容id", paramType = "path", required = true, dataType = "int")
            })
    @PostMapping("/{contentId:[0-9]+}/delete")
    public void deleteContent(@PathVariable("contentId") Integer contentId) {
        contentService.deleteContent(contentId);
    }

    @ApiOperation("查询内容列表")
    @GetMapping("/list")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "当前页", dataType = "int"),
            @ApiImplicitParam(name = "pageSize", value = "每页记录数", dataType = "int")
    })
    public JsonPage<ContentListVO> listContent(
            @RequestParam(required = false, defaultValue = PageConstant.DEFAULT_PAGE) Integer page,
            @RequestParam(required = false, defaultValue = PageConstant.DEFAULT_PAGE_SIZE) Integer pageSize) {
        PageInfo<ContentListBO> boPageInfo = contentService.list(page, pageSize);
        PageInfo<ContentListVO> voPageInfo = contentAssemble.pageBoListAssemblePageVOList(boPageInfo);
        return JsonPage.restPage(voPageInfo);
    }

    @ApiOperation("根据内容id查询内容详情")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "contentId", value = "内容id", required = true, dataType = "int")
    })
    @GetMapping("/detail")
    public ContentVO detail(@RequestParam("contentId") Integer contentId) {
        ContentBO contentBO = contentService.selectContentById(contentId);
        ContentVO contentVO = contentAssemble.boAssembleVO(contentBO);
        return contentVO;
    }
}
